import axios from 'axios'
import router from './router'

const http = axios.create({
    baseURL: process.env.VUE_APP_API_URL || 'api',
    // baseURL: process.env.VUE_APP_DEBUGGER_API_URL || 'api',
    // baseURL: 'http://localhost:510/api'
})

http.interceptors.request.use(config => {
    if (sessionStorage.token) {
        config.headers.authorization = 'Bearer ' + sessionStorage.token;
    }
    return config;
}, err => {
    return Promise.reject(err);
})

http.interceptors.response.use(res => {
    return res;
}, err => {
    if (err.response.data.message) {
        alert(err.response.data.message)
    }
    if (err.response.status === 401) {
        router.push('/login');
    }
    return Promise.reject(err);
})

export default http